﻿using Xceed.Wpf.Toolkit.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.ComponentModel;  // for canceleventargs
using System.Runtime.InteropServices;
using System.Windows.Interop;
using Signage01.dao;

namespace Signage01.Dialogs
{
    /// <summary>
    /// Interaction logic for AddScreenWindow.xaml
    /// </summary>
    /// 

    public partial class AddScreenWindow : Window
    {
        // start window closing
       
        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        // end window closing
        private List<String> listScreenDim = new List<String> { "240x320", "480x640", "480x800", "540x960", "600x800", "600x1280","1000x800","800x1000","500x400"};
        private List<Screen> listScreen;

       
        public AddScreenWindow()
        {
            InitializeComponent();
            //resCombobox.Items.Add("240x320");
            resCombobox.ItemsSource = listScreenDim;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            //ListBoxOption.SelectedIndex = 0;
            //this.listScreen = listScreen;
            
        }

        
        
        public AddScreenWindow(List<Screen> listScreen)
        {
            InitializeComponent();
            //resCombobox.Items.Add("240x320");
            resCombobox.ItemsSource = listScreenDim;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            //ListBoxOption.SelectedIndex = 0;

            this.listScreen = listScreen;
        }


        private void ListBoxItem_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            //screen option

        }

        private void ListBoxItem_MouseLeftButtonUp_2(object sender, MouseButtonEventArgs e)
        {
            //screen divisions
        }

        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            //screen resolution combobox

            //string selectedRes = resCombobox.Items.GetItemAt(resCombobox.SelectedIndex) as string;
            //Console.WriteLine(selectedRes);s
            //Console.WriteLine(resCombobox.Items.GetItemAt(resCombobox.SelectedIndex));
            screenName.Text = "S" + resCombobox.Items.GetItemAt(resCombobox.SelectedIndex);
            string temp = (string)resCombobox.Items.GetItemAt(resCombobox.SelectedIndex);
            string[] splittedString = temp.Split('x');

            widthSpinner.Value = Convert.ToDecimal(splittedString[0]);
            heightSpinner.Value = Convert.ToDecimal(splittedString[1]);
        }
       
        private void ListBoxScreenItem_Selected_1(object sender, RoutedEventArgs e)
        {
            
        }
        /*
        private void ListBoxScreenDivisionItem_Selected_1(object sender, RoutedEventArgs e)
        {
            //Console.WriteLine("div selected");
            ListBoxScreenDivisionItem.Background = Brushes.Aquamarine;
        }
        */
        private void storeScreenObject()
        {
            /*
            string serializationFile = "resources/screen/screen.bin";
            //List<Screen> listScreen;

            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.Open))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    this.listScreen = (List<Screen>)bformatter.Deserialize(stream);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                this.listScreen = new List<Screen>();
            }
            */
            // creating a new object from the user input, adding the object to the list
            Screen screen = new Screen();
            screen.ScreenName = screenName.Text;
            screen.Width = Convert.ToInt32(widthSpinner.Value);
            screen.Height = Convert.ToInt32(heightSpinner.Value);
            //this.listScreen.Add(screen);
            
            ScreenDAO screenDAO = ScreenDAO.getSingleTon();
            this.listScreen = screenDAO.getAll();
            this.listScreen.Add(screen);
            screenDAO.saveListToFile(this.listScreen);

            
            /*
            using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
            {
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                bformatter.Serialize(stream, listScreen);
                stream.Close();
            }
            */
          
            
        }// storeSceneObject function end

        private void buttonCancel_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        /*
        private void buttonPrevious_Click_1(object sender, RoutedEventArgs e)
        {
            gridScreen1.Visibility = Visibility.Visible;
            gridScreen2.Visibility = Visibility.Collapsed;
            ListBoxOption.SelectedIndex = 0;
        }
        */
        private void buttonFinish_Click_1(object sender, RoutedEventArgs e)
        {
            ScreenDAO screenDAO = ScreenDAO.getSingleTon();
            if (screenDAO.screenExists(screenName.Text))
            {
                MessageBox.Show("Screen already exists");
                return;
            }
            if (screenName.Text=="")
            {
                MessageBox.Show("No screen resolution selected!");
                return;
            }

            storeScreenObject();
            this.DialogResult = true;
            AddScreenWindow_Closing(sender, null);
            this.Close();
        }

        private void buttonCancel2_Click_1(object sender, RoutedEventArgs e)
        {
            AddScreenWindow_Closing(sender, null);
            this.DialogResult = false;
            this.Close();
        }
        /*
        private void buttonNext_Click_1(object sender, RoutedEventArgs e)
        {
            gridScreen1.Visibility = Visibility.Collapsed;
            gridScreen2.Visibility = Visibility.Visible;
            ListBoxOption.SelectedIndex = 1;
        }
        
        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {

        }
        */
        private void AddScreenWindow_Closing(object sender, CancelEventArgs e)
        {
            this.Owner.ShowActivated = true;
        }

        private void Window_SourceInitialized_1(object sender, EventArgs e)
        {

        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            var hwnd = new WindowInteropHelper(this).Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
        }

    }
}
